﻿//准备ExtJS环境
Ext.require
(
    [
        'Ext.form.field.File',
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.state.*',
        'Ext.form.*'
    ]
);

//定义用户表格控件
Ext.define
(
    'ProjectLabour.Grid',
    {
        extend: 'Ext.grid.Panel',

        //初始化组件
        initComponent: function ()
        {
            Ext.apply
            (
                this,
                {
                    listeners:
                    {
                        //设置两个指头的滚动
                        render: Common.ScrollableRender
                    },
                    dockedItems:
                    [
                        {
                            xtype: 'toolbar',
                            height: 36,
                            items:
                            [
                                {
                                    iconCls: 'icon-refresh',
                                    text: '刷新',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '刷新数据',
                                    handler: this.onRefreshClick
                                },
                                '->',
                                {
                                    iconCls: 'icon-approve',
                                    text: '批准',
                                    itemId: 'button-approve',
                                    scope: this,
                                    disabled: true,
                                    hidden: false,
                                    tooltip: '批准养护工程量签审申请',
                                    handler: this.onApproveClick
                                },
                                {
                                    iconCls: 'icon-reject',
                                    text: '拒绝',
                                    itemId: 'button-reject',
                                    scope: this,
                                    disabled: true,
                                    hidden: false,
                                    tooltip: '拒绝养护工程量签审申请',
                                    handler: this.onRejectClick
                                }
                            ]
                        }
                    ]
                }
            );
            this.callParent();
            this.getSelectionModel().on('selectionchange', this.onSelectionChange, this);
        },

        //处理选中变化事件
        onSelectionChange: function (selectionModel, selections)
        {
            //判断是否选中
            if (selections.length === 0)
            {
                //禁用删除按钮
                this.down('#button-delete').setDisabled(true);
                //禁用编辑按钮
                this.down('#button-edit').setDisabled(true);
                //禁用上传按钮
                this.down('#button-submit').setDisabled(true);
            }
            else
            {
                //启用删除按钮
                this.down('#button-delete').setDisabled(false);
                //启用编辑按钮
                this.down('#button-edit').setDisabled(false);
                //启用上传按钮
                this.down('#button-submit').setDisabled(false);
            }
        },

        //处理选中养护变化事件
        onSelectionChange: function (selectionModel, selections)
        {
            //判断是否选中养护
            if (selections.length === 0)
            {
                //禁用批准按钮
                this.down('#button-approve').setDisabled(true);
                //禁用拒绝按钮
                this.down('#button-reject').setDisabled(true);
            }
            else
            {
                //获得选中的行
                var selection = this.getView().getSelectionModel().getSelection()[0];

                //启用批准按钮
                this.down('#button-approve').setDisabled(false);
                //启用拒绝按钮
                this.down('#button-reject').setDisabled(false);
            }
        },

        //处理刷新按钮点击事件
        onRefreshClick: function ()
        {
            //刷新数据
            this.store.load();
        },

        //处理批准按钮点击事件
        onApproveClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得所有选中的行
            var selection = this.getView().getSelectionModel().getSelection();

            //判断是否选中行
            if (selection.length > 0)
            {
                //记录所有的编号
                var labours = [];

                //循环添加
                for (var index in selection)
                {
                    //添加记录
                    labours.push(selection[index].data.LabourID);
                }

                //询问是否执行操作
                Ext.Msg.show
                (
                    {
                        title: '工程人工签审请求批准',
                        width: 320,
                        height: 240,
                        msg: '确定要批准选中工程人工的签审请求吗?',
                        buttons: Ext.Msg.YESNO,
                        icon: Ext.Msg.QUESTION,
                        fn: function (button)
                        {
                            //判断是否确定删除 
                            if (button == 'yes')
                            {
                                //创建请求对象
                                Ext.Ajax.request
                                (
                                    {
                                        url: Common.MakeAbsoluteUrl('ExecuteSign'),
                                        method: HttpMethod.Post,
                                        params:
                                        {
                                            labours: labours, passed: true
                                        },
                                        callback: function (options, success, response)
                                        {
                                            //处理错误
                                            if (Common.HandleError(options, success, response))
                                            {
                                                //加载数据
                                                store.load();
                                            }
                                        }
                                    }
                                );
                            }
                        }
                    }
                );
            }
        },

        //处理拒绝按钮点击事件
        onRejectClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得所有选中的行
            var selection = this.getView().getSelectionModel().getSelection();

            //判断是否选中行
            if (selection.length > 0)
            {
                //记录所有的编号
                var labours = [];

                //循环添加
                for (var index in selection)
                {
                    //添加记录
                    labours.push(selection[index].data.LabourID);
                }

                Ext.Msg.show
                (
                    {
                        title: '工程人工签审请求拒绝',
                        width: 320,
                        height: 240,
                        msg: '确定要拒绝选中工程人工的签审请求吗?',
                        buttons: Ext.Msg.YESNO,
                        icon: Ext.Msg.QUESTION,
                        fn: function (button)
                        {
                            //判断是否确定删除 
                            if (button == 'yes')
                            {
                                //创建请求对象
                                Ext.Ajax.request
                                (
                                    {
                                        url: Common.MakeAbsoluteUrl('ExecuteSign'),
                                        method: HttpMethod.Post,
                                        params:
                                        {
                                            labours: labours, passed: false
                                        },
                                        callback: function (options, success, response)
                                        {
                                            //处理错误
                                            if (Common.HandleError(options, success, response))
                                            {
                                                //加载数据
                                                store.load();
                                            }
                                        }
                                    }
                                );
                            }
                        }
                    }
                );
            }
        }
    }
);

//挂接环境准备完毕事件
Ext.onReady
(
    function ()
    {
        //初始化快速提示
        Ext.QuickTips.init();

        //设置状态保存提供者（当前设置为Cookie）
        Ext.state.Manager.setProvider( Ext.create( 'Ext.state.CookieProvider', { path: _Current_ActionUrl } ) );

        //初始化附加验证类型
        Common.InitAdditionalValidation();

        //创建数据存储对象
        var store = Ext.create
        (
            'Ext.data.Store',
            {
                pageSize: 20,
                autoSync: true,
                autoLoad: true,
                root: 'data',
                remoteFilter: true,
                remoteSort: true,
                model: 'ProjectLabourModel',
                proxy:
                {
                    type: 'ajax',
                    batchActions: false,
                    url: Common.MakeAbsoluteUrl('SignSelect'),
                    reader:
                    {
                        type: 'json',
                        root: 'data'
                    },
                    listeners:
                    {
                        exception: function (proxy, response, operation, options)
                        {
                            //处理错误
                            Common.HandleProxyError(proxy, response, operation, options);
                        }
                    }
                }
            }
        );

        //创建选择模型
        var selectionModel = Ext.create('Ext.selection.CheckboxModel');

        //创建表格
        Ext.create
        (
            'ProjectLabour.Grid',
            {
                store: store,
                selModel: selectionModel,
                stateful: true,
                stateId: 'projectlabour-confirm-state-grid',
                height: 600,
                width: '100%',
                id: 'projectlabour-grid',
                title: '',
                renderTo: 'data-grid',
                columnLines: true,
                viewConfig:
                {
                    stripeRows: true,
                    loadingText: '数据加载中...',
                    singleSelect: true
                },
                //创建分页工具栏
                bbar: Ext.create
                (
                    'Ext.PagingToolbar',
                    {
                        store: store,
                        displayInfo: true,
                        displayMsg: '第 {0} - 第 {1} 条记录，共 {2} 条记录',
                        emptyMsg: ''
                    }
                ),
                columns:
                [
                    {
                        text: '单位名称',
                        width: 160,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Organization',
                        hideable: false
                    },
                    {
                        text: '项目编号',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'ProjectCode',
                        hideable: false
                    },
                    {
                        text: '项目名称',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'ProjectName',
                        hideable: false
                    },
                    {
                        text: '状态',
                        width: 60,
                        sortable: true,
                        align: 'center',
                        dataIndex: 'State',
                        renderer: Common.RenderProjectDataState
                    },
                    {
                        text: '人工属性',
                        width: 80,
                        sortable: true,
                        align: 'center',
                        dataIndex: 'LabourType'
                    },
                    {
                        text: '工种',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Job'
                    },
                    {
                        text: '人工数量',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer('0,000.00'),
                        dataIndex: 'Quantity'
                    },
                    {
                        text: '人工单价',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer('0,000.00'),
                        dataIndex: 'Price'
                    },
                    {
                        text: '小计',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer('0,000.00'),
                        dataIndex: 'Amount'
                    },
                    {
                        text: '施工日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'ApplicationDate'
                    },
                    {
                        text: '填报人',
                        width: 80,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Applicant'
                    },
                    {
                        text: '填报日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'CreateDate'
                    },
                    {
                        text: '审核人',
                        width: 80,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Auditor'
                    },
                    {
                        text: '审核日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'AuditedDate'
                    },
                    {
                        text: '延期填报',
                        width: 80,
                        sortable: true,
                        align: 'center',
                        renderer: renderGridReadonlyCheckBox,
                        dataIndex: 'IsDelay'
                    },
                    {
                        text: '备注',
                        xtype: 'templatecolumn',
                        tpl: '<span title="{Memo}">{Memo}</span> ',
                        width: 160,
                        minWidth: 160,
                        flex: 1,
                        sortable: false,
                        align: 'left',
                        dataIndex: 'Memo',
                        hideable: true
                    }
                ]
            }
        );

        $(window).bind
        (
            "resize",
            function ()
            {
                //调整表格宽度
                Common.AdjustGridWidth('projectlabour-grid', 'body_content');
            }
        );

        //调整表格宽度
        Common.AdjustGridWidth('projectlabour-grid', 'body_content');
    }
);
